
<form class="layui-form" action="">

    <div class="layui-form-item">
      <label class="layui-form-label">旧的密码</label>
      <div class="layui-input-block">
        <input type="text" name="password" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
      </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">新的密码</label>
        <div class="layui-input-block">
          <input type="text" name="newpass" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
      </div>

      <div class="layui-form-item">
        <label class="layui-form-label">确认密码</label>
        <div class="layui-input-block">
          <input type="text" name="checkpass" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
      </div>

      <div class="layui-form-item">
        <div class="layui-input-block">
          <button type="submit" class="layui-btn" lay-submit lay-filter="setPassword">立即提交</button>
        </div>
      </div>


</form>


<script>

layui.use(['form'], function(){

  var form = layui.form;
    // 提交事件
  form.on('submit(setPassword)', function(data){

    var field = data.field; // 获取表单字段值
    // 显示填写结果，仅作演示用
    // layer.alert(JSON.stringify(field), {
    //   title: '当前填写的字段值'
    // });

    if(field.newpass != field.checkpass){
        layer.msg("输入密码不一致");
        return false;
    }
 
    $.post("{:url('/admin/system.admin/password')}",
        {"password":field.password,"newpass":field.newpass},
        function(res){
            console.log(res);
            if(res.code==1){
                layer.msg(res.msg,function(){
                    window.parent.location.href=res.url;
                });
            }else{
                layer.msg(res.msg);
            }
        }
    )



    // 此处可执行 Ajax 等操作
    // …
    return false; // 阻止默认 form 跳转
  });


})

</script>